var vm = new Vue({
    el: '#login',
    data: {
        userId: "",
        userPass: "",
        userCode: "",
        loginErr: "",
        showVerifyCode: true,
    },
    computed: {

    },
    methods: {
        checkInfo: function() {
            if(this.userId == "") {
                this.loginErr = "工号不能为空";
                return true;
            }
            if(this.userPass == "") {
                this.loginErr = "密码不能为空";
                return true;
            }
            return false;
        },
        checkVerifyCode: function() {
            //检查校验码
            var verifyCode = this.strTrim(this.userCode);
            if(verifyCode == null || verifyCode == '') {
                this.loginErr = "校验码不能为空";
                return true;
            }
            return false;
        },
        toLogin: function() {
            //如果报错就不能
            if(this.checkInfo()) {
                return;
            }
            if(this.needVerifyCode) {
                return;
            }
            this.loginErr = "";
            $("#loginForm").submit();
        },
        strTrim: function(str) {
            return str.replace(/(^\s*)|(\s*$)/g, "");
        },
        userNameBlur: function() {
            if(this.userId == "") {
                return;
            }
            //动态验证码是否显示
            var userNameVal = this.strTrim(this.userId),
                _self = this;
            $.ajax({
                type: 'POST',
                url: 'needVerifyCode',
                data: 'username=' + encodeURIComponent(userNameVal),
                dataType: 'json',
                success: function(data) {
                    if(data && data.needVerifyCode) {
                        if(!(_self.needVerifyCode)) {
                            _self.needVerifyCode = true;
                            _self.showVerifyCode = true;
                        }
                    } else if(data && !data.needVerifyCode) {
                        if(_self.needVerifyCode) {
                            _self.needVerifyCode = false;
                            _self.showVerifyCode = false;
                        }
                    }
                },
                error: function(data) {}
            });
        },
        getVcode: function() {
            //$('#verifyCodeServiceUrl').val()
            var codeserviceUrl = $('#verifyCodeServiceUrl').val();
            this.verifyImgSrc = codeserviceUrl + "/imageCode.htm?uuid=" + this.uuid + "&yys=" + new Date().getTime();;
        },
        initShowVerifyCode: function() {
        	var _self = this;
            //初始化图片验证码是否显示
            var needVerifyCodeVal = $('#needVerifyCode').val()
            if(needVerifyCodeVal == 'true') {
                _self.showVerifyCode = true;
            } else {
                _self.showVerifyCode = false;
            }
        }
    },
    ready: function() {
        this.getVcode(); //获取校验码信息
        this.initShowVerifyCode();
    }
});